package com.greenant.store.utils;

import org.json.JSONException;
import org.json.JSONObject;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVInstallation;
import com.avos.avoscloud.AVPush;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.PushService;
import com.avos.avoscloud.SendCallback;
import com.greenant.store.activity.MainActivity;
import com.greenant.store.broadcast.MallManagerReceiver;

import android.content.Context;

public class NotificationUtil {
	/**
	 * 发�?�给特定设备通知
	 * 
	 * @param installId
	 * @since 3.8
	 */
	public static void sendMsg(String installId) {
		AVQuery pushQuery = AVInstallation.getQuery();
		// 假设 THE_INSTALLATION_ID 是保存在用户表里�? installationId�?
		// 可以在应用启动的时�?�获取并保存到用户表
		pushQuery.whereEqualTo("installationId", installId);
		AVPush.sendMessageInBackground("message to installation", pushQuery, new SendCallback() {
			@Override
			public void done(AVException e) {

			}
		});
	}

	public static void sendPrivateMsg(String msg, String msgTypeId, int msgType, String userId) {
		final AVPush push = new AVPush();

		try {

			JSONObject data = new JSONObject("{\"action\": \"" + MallManagerReceiver.ACTION + "\", " + "\"msg\": \"" + msg
					+ "\"," + "\"time\": " + System.currentTimeMillis() + "," + "\"msgTypeId\": \"" + msgTypeId + "\","
					+ "\"msgType\": " + msgType + "," + "\"userId\": \"" + userId + "\"" + "}");

			push.setData(data);

			AVQuery<AVUser> user = AVUser.getQuery();
			user.getInBackground(userId, new GetCallback<AVUser>() {

				@Override
				public void done(AVUser arg0, AVException arg1) {
					if (arg1 == null && arg0 != null) {
						String installationId = arg0.getString("installationId");
						push.setCloudQuery(
								"select * from _Installation where installationId ='" + installationId + "'");
						push.sendInBackground(new SendCallback() {

							@Override
							public void done(AVException e) {

							}
						});
					}
				}
			});

		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 注册消息接收
	 * 
	 * @param context
	 * @since 3.8
	 */
	public static void registerNotificationReceiver(Context context) {
		PushService.setDefaultPushCallback(context, MainActivity.class);
		if (AVUser.getCurrentUser() != null) {
			PushService.subscribe(context, "Order_" + AVUser.getCurrentUser().getObjectId(), MainActivity.class);

		}
		PushService.subscribe(context, "public", MainActivity.class);
	}

	/**
	 * 取消注册消息接收
	 * 
	 * @param context
	 * @since 3.8
	 */
	public static void unregisterNotificationReceiver(Context context) {
		PushService.setDefaultPushCallback(context, MainActivity.class);
		unRegisterNotificationWhenUserChange(context);
		PushService.unsubscribe(context, "public");
		// �?订之后需要重新保�? Installation
		AVInstallation.getCurrentInstallation().saveInBackground();
	}

	/**
	 * 切换用户之前取消注册消息接收
	 * 
	 * @param context
	 * @since 3.8
	 */
	public static void unRegisterNotificationWhenUserChange(Context context) {
		if (AVUser.getCurrentUser() != null) {
			PushService.unsubscribe(context, "Order_" + AVUser.getCurrentUser().getObjectId());

		}
	}
}
